const slider = document.querySelector(".slider");
const btns = document.querySelectorAll("a");

const setSlider = (width, x = 0) => {
    slider.style.width = `${width}px`;
    slider.style.transform = `translateX(${x}px)`;
};

btns.forEach((btn, index) => {
    // 默认选中第一个
    if (index === 0) {
        setSlider(btn.offsetWidth, btn.offsetLeft);
    }

    btn.addEventListener("click", e => {
        const target = e.target;
        setSlider(target.offsetWidth, target.offsetLeft);
    });
});
